package com.example.yin.controller;

import com.example.yin.common.SuccessMessage;
import com.example.yin.domain.History;
import com.example.yin.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class HistoryController {

    @Autowired
    private HistoryService historyService;

    @GetMapping(value = "/history")
    public Object getHistorywithTimestampdesc(@RequestParam("id") Integer id){
        List<History> res = historyService.getHisotrybyuserId(id,0);

        return new SuccessMessage<List<History>>("查询成功", res).getMessage();

    }

    @GetMapping(value = "/history/")
    public  Object getHistorywithTimesdesc(@RequestParam("id") Integer id){
        List<History> res = historyService.getHisotrybyuserId(id,1);

        return new SuccessMessage<List<History>>("查询成功", res).getMessage();
    }

    @PostMapping(value = "/history/update")
    public Object update(@RequestParam("userId") Integer userId,@RequestParam("songId") Integer songId,@RequestParam("songName") String songName){
        boolean v = historyService.vertify(userId,songId);

        History history = new History();

        history.setUserId(userId);
        history.setSongId(songId);
        history.setTimestamp(new Date());
        history.setSongName(songName);

        if(v){
            historyService.update(history);
        }
        else{
            historyService.insert(history);
        }
        return new SuccessMessage<List<History>>("成功").getMessage();
    }

}
